package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.ShelfService;
import com.ctgu.fastleader.service.WareHouseService;
import com.ctgu.fastleader.vo.PianQu;
import com.ctgu.fastleader.vo.Shelf;
import com.ctgu.fastleader.vo.WareHouse;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/warehouse")
public class WarehouseController {
    @Resource
    WareHouseService wareHouseService;

    @Resource
    ShelfService shelfService;

    @ResponseBody
    @RequestMapping("/findAllWarehouse")
    public List<WareHouse> findAllWarehouse(){
        return wareHouseService.findAllWarehouse();
    }

    @RequestMapping("/toShelf")
    public String toShelf(Integer houseid,String housename, Model model){
        model.addAttribute("houseid",houseid);
        model.addAttribute("housename",housename);
        return "forward:/page/toShelf";
    }

    @ResponseBody
    @RequestMapping("/findAllShelf")
    public List<PianQu> findAllShelf(Integer houseid){
        List<String> shelfid = shelfService.findAllShelfIdByHouseId(houseid);
        List<String> pianqu = new ArrayList<>();

        //分割得到片区
        for(String el : shelfid){
            StringTokenizer str = new StringTokenizer(el,"-");
            pianqu.add(str.nextToken());
        }
        //去重
        HashSet<String> removal = new HashSet<>(pianqu);
        pianqu.clear();
        pianqu.addAll(removal);

        List<PianQu> list = new ArrayList<>();

        //找到每一个片区的货架
        for(String el : pianqu){
            List<Shelf> shelfList = shelfService.findAllShelfByHouseIdAndPianQu(houseid,el);
            PianQu pianQu = new PianQu(el,shelfList);
            list.add(pianQu);
        }
        return list;
    }

    @ResponseBody
    @RequestMapping("/judgeWarehouse")
    public String judgeWarehouse(String housename){
        Integer houseid = wareHouseService.selectHouseIdByHouseName(housename);
        if(houseid == null){
            return "success";
        }else{
            return "fail";
        }
    }

    @ResponseBody
    @RequestMapping("/judgeWarehouseAndInfo")
    public String judgeWarehouseAndInfo(Integer houseid, String housename){
        Integer houseId = wareHouseService.selectHouseIdByHouseName(housename);
        if(houseId == null){
            return "success";
        }else if(!houseid.equals(houseId)){
            return "fail";
        }else{
            return "success";
        }
    }

    @ResponseBody
    @RequestMapping("selectHouseInfoByHouseName")
    public String selectHouseInfoByHouseName(String housename){
        String houseinfo = wareHouseService.selectHouseInfoByHouseName(housename);
        return houseinfo;
    }


    @ResponseBody
    @RequestMapping("/addWarehouse")
    public String addWarehouse(HttpServletRequest request){
        String housename = request.getParameter("housename");
        String houseinfo = request.getParameter("houseinfo");
        WareHouse wareHouse = new WareHouse(housename,houseinfo);
        wareHouseService.insert(wareHouse);
        return "success";
    }

    @RequestMapping("toUpdateWarehouse")
    public String toUpdateWarehouse(Integer houseid,String housename,Model model){
        model.addAttribute("houseid",houseid);
        model.addAttribute("housename",housename);
        return "forward:/page/toUpdateWarehouse";
    }

    @ResponseBody
    @RequestMapping("updateWarehouse")
    public String updateWarehouse(Integer houseid,String housename,String houseinfo){
        WareHouse wareHouse = new WareHouse(houseid,housename,houseinfo);
        wareHouseService.updateByPrimaryKeySelective(wareHouse);
        return "success";
    }


}
